package com.imooc.security.core.validate.code;

import com.imooc.security.core.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author sherry
 * @Description
 * @Date Create in 2019-03-20
 * @Modified By:
 */
@Configuration
public class ValidateCodeBeanConfig {

    @Autowired
    private SecurityProperties securityProperties;

    @Bean
//    如果用户没有实现接口，且名称为 imageCodeGenerator，则使用系统默认提供的
    @ConditionalOnMissingBean(name = "imageCodeGenerator")
    public ValidateCodeGenerator imageCodeGenerator() {
        ValidateCodeGenerator codeGenerator = new ImageCodeGenerator();
        ((ImageCodeGenerator) codeGenerator).setSecurityProperties(securityProperties);
        return codeGenerator;
    }

    @Bean
//    如果用户没有实现接口，且名称为 smsCodeGenerator，则使用系统默认提供的
    @ConditionalOnMissingBean(name = "smsCodeGenerator")
    public ValidateCodeGenerator smsCodeGenerator() {
        ValidateCodeGenerator codeGenerator = new SmsCodeGenerator();
        ((SmsCodeGenerator) codeGenerator).setSecurityProperties(securityProperties);
        return codeGenerator;
    }

    @Bean
    @ConditionalOnMissingBean(SmsCodeSender.class)
    public SmsCodeSender smsCodeSender(){
        return new DefaultSmsCodeSender();
    }

}
